package org.example.config;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect//切面：包含切点和通知
@Component
@Slf4j
public class UserAspect {

    @Pointcut("execution(* org.example.controller.User*.test*(..))")
    public void intercept(){

    }

    @Before("intercept()")
    public void before(){
        log.info("controller方法执行前before");
    }
    @After("intercept()")
    public void after(){
        log.info("controller方法执行后after");
    }
    @AfterReturning("intercept()")
    public void afterReturn(){
        log.info("controller方法返回后afterReturning");
    }
    @AfterThrowing("intercept()")
    public void afterThrowing(){
        log.info("controller方法抛异常后");
    }
    @Around("intercept()")
    public void around(ProceedingJoinPoint joinPoint){
        long start = System.currentTimeMillis();
        try {
            log.info("controller方法around前");
            Object o =joinPoint.proceed();
        } catch (Throwable e) {
            log.error("controller方法around捕获异常", e);
            throw new RuntimeException("controller出错", e);
        }
        long end = System.currentTimeMillis();
        log.info("controller方法around后，执行时间：{}", (end-start)+"毫秒");

    }
}
